package com.acoreful.acf2java.admin.modules.auth.web;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.acoreful.acf2java.admin.modules.auth.service.CaptchaService;

/**
 *	验证码
 */
@RequestMapping("/captcha")
@Controller
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping(method = RequestMethod.GET)
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String captchaToken = request.getParameter("captchaToken");
        Assert.notNull(captchaToken, "CaptchaToken DOES NOT exist.");

        BufferedImage image = captchaService.createImage(captchaToken);

        response.setContentType("image/jpeg");
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        ImageIO.write(image, "jpg", response.getOutputStream());
    }
}
